<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/6/23
 * Time: 9:46 AM
 */
namespace app\backend\modules\content\controllers;

use app\backend\controllers\BaseController;
use app\backend\modules\content\services\TagService;
use app\common\models\Tag;
use app\common\models\ArticleTag;
use Illuminate\Support\Facades\DB;

class TagController extends BaseController
{
    public function index(){
        return view("backend.content.tag.index");
    }
    public function getList(){
        $search = request()->search;
        $list = (new TagService())->getList($search);
        return $this->successJson('',$list);
    }
    public function detail(){
        return view("backend.content.tag.detail");
    }
    public function getDetail(){
        $id = intval(request()->id);
        $model = Tag::site()->select()
            ->where('id',$id)
            ->first();
        if (empty($model)) {
            return $this->errorJson('未找到数据');
        }
        return $this->successJson('',$model);
    }
    public function create(){
        $data = request()->params;
        $data['site_id'] = \YunCms::siteId();
        $name = $data['name'];
        $oldData = Tag::getByName($name);
        if($oldData['id']){
            return $this->errorJson('标签名称已存在，不能添加相同名称的标签');
        }
        $model = new Tag();
        $model->fill($data);
        $validator = $model->validator();
        if ($validator->fails()) {
            return $this->errorJson($validator->messages());
        } else {
            $model->save();
            return $this->successJson();
        }
    }
    public function update(){
        $data = request()->params;
        $id = $data['id'];
        $model = Tag::find($id);
        if (empty($model)) {
            $this->errorJson('未找到数据');
        }
        $name = $data['name'];
        if($name!=$model['name']){
            //名称修改了，检查是否和已添加的重复
            $oldData = Tag::getByName($name);
            if($oldData['id']){
                return $this->errorJson('标签名称已存在，不能添加相同名称的标签');
            }
        }
        $model->fill($data);
        $validator = $model->validator();
        if ($validator->fails()) {
            return $this->errorJson($validator->messages());
        } else {
            $model->save();
        }
        return $this->successJson();
    }
    public function delete(){
        $id = request()->id;
        $model = Tag::find($id);
        if(!$model){
            return $this->errorJson('未找到数据');
        }
        $articleTag = ArticleTag::select()
            ->where('tag_id',$id)
            ->first();
        if($articleTag['id']){
            return $this->errorJson('该标签存在关联文章，不能删除');
        }
        DB::transaction(function() use ($model,$id){
            $model->delete();
            ArticleTag::where('tag_id',$id)->delete();
        });
        return $this->successJson();
    }
    public function changeStatus()
    {
        $id = request()->id;
        $model = Tag::site()->select()
            ->where('id',$id)
            ->first();
        if (empty($model)) {
            $this->errorJson('未找到数据');
        }
        $status=Tag::STATUS_NORMAL;
        if($model->status==Tag::STATUS_NORMAL){
            $status= Tag::STATUS_BAN;
        }
        $model->status = $status;
        $model->save();
        return $this->successJson();
    }
    /**
     * 检索分类
     */
    public function query(){
        $kwd = trim(request()->kwd);
        $list = Tag::site()->select(['id','name'])
            ->where('status',Tag::STATUS_NORMAL);
        if($kwd){
            $list = $list->where('name','like','%'.$kwd.'%');
        }
        $list = $list
            ->orderBy('id', 'desc')
            ->take(1000)
            ->get();
        return $this->successJson('',$list);
    }
}